package com.wang.learn.cloudfeture.pojo;

import com.wang.learn.cloudfeture.demo.ConcurrentTest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * ClassName RunnableDemo
 *
 * @author wang
 * Date 2022/4/1 10:26
 */
@Data
@NoArgsConstructor
public class LockDemo implements Runnable {

    private CountDownLatch countDownLatch;

    private ReentrantLock lock = new ReentrantLock();

    private int n = 0;

    @Override
    public void run() {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        lock.lock();
        try {
            // 实现有序打印
            ConcurrentTest.num++;
            System.out.println(ConcurrentTest.num);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

}
